package com.just521.xinshoutong.database;

import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class NoteDBCtrl {
	//---------------internal define-------------------
	public class DBRequestParam{
		public DBAccessListener listener;
		public String rstjson;
		public String task;
	}
	private static final int DB_COMPLETE = 1;  
	private static final int DB_ERROR = 2;  
	public interface DBAccessListener{
		public void onDBSuccess(DBRequestParam param);
		public void onDBError(DBRequestParam param);
	}
	
	public DBRequestParam getRequestParam(){
		return new DBRequestParam();
	}
	
	//---------------native method-------------------
	public native String  stringFromJNI();
	private native String  getNoteSummaryList(int begin,int count);
	private native String  getTagList();
	private native String  getNoteSummaryListByDate(long date);
	private native String  getNoteDetailByUUID(String uuid);
	private native int addNote(String notejson,String summaryjson,String uuid);
	private native int addTags(String tagsjson);
	private native int modifyNote(String notejson,String summaryjson,String uuid);
	//---------------local method-------------------
	public void getNoteSummaryList_Local(final DBRequestParam param,final int begin,final int count){
		new Thread(new Runnable() {  
            public void run() {  
            	String json=getNoteSummaryList(begin,count);
            	processResultJson(param,json);
            }  
        }).start(); 
	}
	public void getTagList_Local(final DBRequestParam param){
		new Thread(new Runnable() {  
            public void run() {  
            	String json=getTagList();
            	processResultJson(param,json);
            }  
        }).start(); 
	}
	public void getNoteSummaryListByDate_Local(final DBRequestParam param,final long date){
		new Thread(new Runnable() {  
            public void run() {  
            	String json=getNoteSummaryListByDate(date);
            	processResultJson(param,json);
            }  
        }).start(); 
	}
	public void getNoteDetailByUUID_Local(final DBRequestParam param,final String uuid){
		new Thread(new Runnable() {  
            public void run() {  
            	String json=getNoteDetailByUUID(uuid);
            	processResultJson(param,json);
            }  
        }).start(); 
	}
	public void addNote_Local(final DBRequestParam param,final String notejson,final String summaryjson,
			final String uuidin){
		new Thread(new Runnable() {  
            public void run() {  
            	int ret=addNote(notejson,summaryjson,uuidin);
            	if(ret!=0){
            		int a=1;
            	}
            	processResultJson(param,null);
            }  
        }).start(); 
	}
	public void addTags_Local(final DBRequestParam param,final String tagsjson){
		new Thread(new Runnable() {  
            public void run() {  
            	int ret=addTags(tagsjson);
            	if(ret!=0){
            		int a=1;
            	}
            	processResultJson(param,null);
            }  
        }).start(); 
	}
	public void modifyNote_Local(final DBRequestParam param,final String notejson,final String summaryjson,
			final String uuidin){
		new Thread(new Runnable() {  
            public void run() {  
            	int ret=modifyNote(notejson,summaryjson,uuidin);
            	if(ret!=0){
            		int a=1;
            	}
            	processResultJson(param,null);
            }  
        }).start(); 
	}
	
	//---------------frame-------------------
	static {
        System.loadLibrary("trunkRtong");
        SDCardAccessor.getNoteDataBaseFullPath();
    }
	private void processResultJson(DBRequestParam param,String json){
		Message msg=new Message();
    	if(json!=null && json.contains("err")){
    		param.rstjson=json.split(":")[1];
    		msg.what=DB_ERROR;

    	}else{
    		
    		param.rstjson=json;
    		msg.what=DB_COMPLETE;
    	}
    	msg.obj=param;
    	mhandler.sendMessage(msg);
	}
	private static Handler  mhandler=new Handler() {
		@Override  
        public void handleMessage(Message msg) {  
			DBRequestParam temp = (DBRequestParam)msg.obj;
        	if(temp.listener==null){
        		return;
        	}
            switch (msg.what) {  
           
            case DB_COMPLETE:  
            	temp.listener.onDBSuccess(temp);  
                break;  
            case DB_ERROR:  
            	temp.listener.onDBError(temp);   
                break; 
            
            default:  
                break;  
            }  
        }  
	};
	public void log_print(String tag,String msg){
		Log.v(tag, msg);
	}
}
